<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<div style="width: 95%;margin: 26px auto;">
    <div id="table_messageAdmin">
    </div>
</div>

<script type="text/html" id="messageOperationTpl">
    <div>
        <button class="layui-btn layui-btn-sm layui-btn-danger" onclick="deleteMessage('{{d.id}}')">删除
        </button>
        <button class="layui-btn layui-btn-sm layui-btn-normal"
                onclick="replay('{{d.sendUserPojo.id}}','{{d.id}}','{{d.messageState}}')">回复
        </button>
        <button class="layui-btn layui-btn-sm layui-btn-warm" onclick="hasRead('{{d.id}}','{{d.messageState}}')">标记已读
        </button>
    </div>
</script>
<script type="text/html" id="messageTypeTpl">
    {{# if(d.messageType == 0 ){ }}
    <span>提建议</span>
    {{# } else if(d.messageType == 1 ) { }}
    <span>求宝贝</span>
    {{# } else {}}
    <span>其他</span>
    {{# }}}
</script>
<script type="text/html" id="messageStateTpl">
    {{# if(d.messageState == 0 ){ }}
    <span style="color: orangered;">未读</span>
    {{# } else if(d.messageState == 1 ) { }}
    <span style="color: dodgerblue;">已读</span>
    {{# }}}
</script>
<script>
    let messageAdminTable = null;
    layui.use('table', function () {
        var table = layui.table;

        messageAdminTable = table.render({
            elem: '#table_messageAdmin'
            , height: 480
            , cellMinWidth: 100
            , url: 'http://localhost/campus/admin/message/getAllMessage'
            , page: {
                layout: ['count']
            }
            , toolbar: true
            , defaultToolbar: [{
                title: '刷新'
                , layEvent: 'refresh'
                , icon: 'layui-icon-refresh-1'
            }, 'filter', 'exports', 'print',]
            , cols: [[ //表头
                {field: 'id', title: '留言ID', width: '15%', align: 'center', sort: true, fixed: 'left'}
                , {
                    field: 'sendUser', title: '留言人姓名', width: '10%', align: 'center'
                    , templet: function (d) {
                        return d.sendUserPojo.realName
                    }
                }
                , {field: 'content', title: '留言内容', width: '22%', align: 'center'}
                , {
                    field: 'messageType', title: '留言类型', width: '10%', sort: true, align: 'center'
                    , templet: '#messageTypeTpl'
                }
                , {
                    field: 'messageState', title: '留言状态', width: '10%', sort: true, align: 'center'
                    , templet: '#messageStateTpl'
                }
                , {field: 'stringTime', title: '留言时间', width: '15%', sort: true, align: 'center'}
                , {field: 'operation', title: '操作', width: '18%', align: 'center', templet: "#messageOperationTpl"}
            ]]
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.count, //解析数据长度
                    "data": res.data.messages //解析数据列表
                };
            }
        });

        table.on('toolbar()', function (obj) {
            switch (obj.event) {
                case 'refresh':
                    messageAdminTable.reload();
                    break;
            }
        });
    });

    function hasRead(messageId, state) {
        if (state !== "1")
            $.ajax({
                url: "http://localhost/campus/admin/message/update"
                , type: "POST"
                , data: "messageId=" + messageId
                , success: function (res) {
                    if (!res.success) {
                        layer.msg("发生异常");
                    } else {
                        messageAdminTable.reload();
                    }
                }
            });
    }

    function deleteMessage(messageId) {
        layer.confirm("确定删除该留言？", {
            title: "提示"
        }, function (index) {
            $.ajax({
                url: "http://localhost/campus/admin/message/delete"
                , type: "POST"
                , data: "messageId=" + messageId
                , success: function (res) {
                    let icon = res.success ? 1 : 2;
                    layer.msg(res.message, {
                        icon: icon, time: 2000
                    });
                    messageAdminTable.reload();
                }
            });
            layer.close(index);
        });
    }


    function replay(userId, messageId, state) {
        let param = {};
        param["receiveUser"] = userId;
        layer.prompt({
            formType: 2,
            title: '回复留言',
            area: ['300px', '100px'] //自定义文本域宽高
        }, function (value, index) {
            param["content"] = value;
            $.ajax({
                url: "http://localhost/campus/admin/message/add"
                , type: "POST"
                , contentType: "application/json"
                , data: JSON.stringify(param)
                , success: function (res) {
                    let icon = res.success ? 1 : 2;
                    layer.msg(res.message, {
                        icon: icon, time: 2000
                    });
                    hasRead(messageId, state)
                }
            });
            layer.close(index);
        });
    }

</script>
</body>
</html>
